package com.wx.lambda.ii.chapter3;

import cn.hutool.core.annotation.AnnotationAttributeValueProvider;
import lombok.Data;

import java.util.ArrayList;
import java.util.Comparator;
import java.util.List;
import java.util.function.BiFunction;
import java.util.function.Predicate;
import java.util.stream.Collectors;

/**
 * 第二章试验
 * 对苹果的集合，进行过滤 根据颜色或者重量
 *
 * @author wang.xun
 * @date 2023/12/05
 */

public class Chapter3Test {
    public static void main(String[] args) {
        ArrayList<Apple> apples = new ArrayList<>();
        initData(apples);
        // 排序
        // 匿名内部类
        apples.sort(new Comparator<Apple>() {
            @Override
            public int compare(Apple o1, Apple o2) {
                return o1.getWeight().compareTo(o2.getWeight());
            }
        });
        System.out.printf("apples step1 : 处理后的集合 %s%n",apples);
        refresh(apples);
        System.out.printf("apples step2 : 处理后的集合 %s%n",apples);
        // lambda 表达式
        apples.sort(((o1, o2) -> o1.getWeight().compareTo(o2.getWeight())));
        System.out.printf("apples step3 : 处理后的集合 %s%n",apples);
        refresh(apples);
        System.out.printf("apples step4 : 处理后的集合 %s%n",apples);

        BiFunction<Apple, Apple, Integer> biFunction = (o1, o2) -> o1.getWeight().compareTo(o2.getWeight());
        apples.sort(biFunction::apply);
        System.out.printf("apples step5 : 处理后的集合 %s%n",apples);
        refresh(apples);
        System.out.printf("apples step5 : 处理后的集合 %s%n",apples);

        apples.sort(Comparator.comparing(Apple::getWeight));
        System.out.printf("apples step6 : 处理后的集合 %s%n",apples);

        refresh2(apples);
        System.out.printf("apples step7 : 处理后的集合 %s%n",apples);
    }

    /**
     * 倒叙1
     *
     * @param apples 苹果
     */
    private static void refresh(ArrayList<Apple> apples) {
        apples.sort(new Comparator<Apple>() {
            @Override
            public int compare(Apple o1, Apple o2) {
                return o2.getWeight().compareTo(o1.getWeight());
            }
        });
    }


    /**
     * 倒叙2
     *
     * @param apples 苹果
     */
    private static void refresh2(ArrayList<Apple> apples) {
        apples.sort(Comparator.comparing(Apple::getWeight).reversed());
    }

    public static void initData(List<Apple> apples) {
    /*
      新建一个List 插入 十条 Apple 对象
     */

        for (int i = 0; i < 10; i++) {
            Apple apple = new Apple();
            apple.setColor("red");
            apple.setWeight(1.0*i);
            apples.add(apple);
        }
        for (int i = 0; i < 10; i++) {
            Apple apple = new Apple();
            apple.setColor("green");
            apple.setWeight(1.0 * i);
            apples.add(apple);
        }

    }



    @Data
    static class Apple{
        /**
         * 颜色
         */
        private String color;
        /**
         * 重量
         */
        private Double weight;
    }

    /**
     * 苹果谓词
     *
     * @author wang.xun
     * @date 2023/12/05
     */
    @FunctionalInterface
    interface ApplePredicate{
        boolean test(Apple apple);
    }

    static class ApplePredicateColorImpl implements ApplePredicate{

        @Override
        public boolean test(Apple apple) {
            return apple.getColor().equals("red");
        }
    }
}
